<template>
  <el-dialog
    @close="cancel"
    :title="title"
    :visible.sync="open"
    width="800px"
    class="dialogClass"
    append-to-body
  >
    <template slot="title">
      <div class="modalTitle">
        <img
          src="@/assets/bluelogo/drawerTitle.png"
          alt=""
          class="titleLogo"
        />{{ title }}
      </div>
    </template>
    <el-form
      ref="form"
      :model="formData"
      :rules="rules"
      size="small"
      label-width="135px"
    >
      <el-form-item label="姓名" prop="visitingName">
        <el-input v-model="formData.visitingName" placeholder="请输入姓名" :maxlength="50"/>
      </el-form-item>
      <el-form-item label="受损金额(元)" prop="damagedAmount">
        <el-input
          v-model.trim="formData.damagedAmount"
          placeholder="请输入受损金额"
          :maxlength="15"
          clearable
          oninput="value=value.replace(/[^0-9.]/g,'')"
        />
      </el-form-item>
      <el-form-item label="区域" prop="location" :maxlength="50">
        <el-autocomplete
          class="inline-input"
          v-model="formData.location"
          :fetch-suggestions="querySearch"
          placeholder="请输入区域"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="稳控责任人" prop="personLiable">
        <el-input v-model="formData.personLiable" placeholder="请输入稳控责任人" :maxlength="50"/>
      </el-form-item>
      <el-form-item label="稳控单位" prop="controlUnit" >
        <el-input v-model="formData.controlUnit" placeholder="请输入内容" />
      </el-form-item>
      <el-form-item label="来访案件" prop="caseName">
        <el-input v-model="formData.caseName" placeholder="请输入案件名称" />
        <!-- <el-select v-model="formData.caseName" placeholder="请选择案件" clearable :maxlength="50">
          <el-option
            v-for="ca in caseOptions"
            :key="ca.id"
            :label="ca.caseName"
            :value="ca.id"
          />
        </el-select> -->
      </el-form-item>
      <el-form-item label="近期信访行为" prop="petitionBehavior">
        <el-input v-model="formData.petitionBehavior"
                  type="textarea"
                  placeholder="请输入近期信访行为"
                  :maxlength="300"
                  show-word-limit
                  :autosize="{ minRows: 7 }" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <div class="dialog-footer">
        <el-button @click="cancel" class="resetButton">取消</el-button>
        <el-button class="searchButton" @click="submitForm">确定</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import {
  listPetition,
  getPetition,
  delPetition,
  addPetition,
  updatePetition,
} from "@/api/warning/petition/petition";
import { listCase } from "@/api/caseManage/case";

import {
  MODALTYPE,
  CASESTATE,
  PAGESIZE,
  PAGENUM,
  DEPTTYPE,
  ROLETYPE,
  CASEEXPORTURL,
} from "@/utils/variable";

import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {getChildXzqh} from "@/api/system/dept";

export default {
  inheritAttrs: false,
  dicts: ["visiting_form"],
  props: {
    modalType: {
      type: String,
      require: true,
    },
    id: {
      type: Number,
    },
  },

  data() {
    return {
      isDisabled: false,
      // 弹出层标题
      title: "",
      temCaseName: "",
      // 是否显示弹出层
      open: false,
      formData: {
        visitingName: "",
        damagedAmount: "",
        location: "",
        personLiable: "",
        controlUnit: "",
        caseName: "",
        petitionBehavior: "",
      },
      rules: {
        visitingName: [
          { required: true, message: "来访人不能为空", trigger: "blur" },
        ],
        damagedAmount: [
          { required: true, message: "受损金额不能为空", trigger: "blur" },
        ],
        location: [
          { required: true, message: "区域不能为空", trigger: "change" },
        ],
        personLiable: [
          { required: true, message: "稳控负责人不能为空", trigger: "blur" },
        ],
        controlUnit: [
          { required: true, message: "稳控单位不能为空", trigger: "blur" },
        ],
        caseName: [
          { required: true, message: "涉及案件不能为空", trigger: "blur" },
        ],
        petitionBehavior: [
          { required: true, message: "近期信访行为不能为空", trigger: "blur" },
        ],
      },
      // 案件选项
      caseOptions: [],
      // 提示框
      districts: [],
    };
  },

  created() {
    listCase().then((response) => {
      return this.caseOptions = response.rows;
    })
    // 区域
    getChildXzqh("321").then((response) => {
      this.districts = response.data;
    });
  },

  computed: {
    resetButtonContent() {
      if (this.modalType == MODALTYPE["详情"]) {
        return "关闭";
      } else {
        return "取消";
      }
    },
  },

  watch: {
    modalType: {
      handler(value) {
        if (value == MODALTYPE["新增"]) {
          this.reset();
          this.open = true;
          this.title = "新增涉案预警记录";
        } else if (value == MODALTYPE["修改"]) {
          this.reset();
          if (this.id) {
            getPetition(this.id).then((response) => {
              this.formData = response.data;
              this.temCaseName = this.formData.caseName;
              this.open = true;
              this.title = "修改涉案预警记录";
            });
          }
        } else if (value == "detail") {
          this.isDisabled = true;
          this.reset();
          if (this.id) {
            getPetition(this.id).then((response) => {
              this.formData = response.data;
              this.open = true;
              this.title = "涉案信访详情";
            });
          }
        }
      },
      immediate: true,
    },
  },

  methods: {
    querySearch(queryString, cb) {
      const results = queryString
        ? this.districts
          .filter(this.createFilter(queryString))
          .map((district) => ({ value: district.qc }))
        : this.districts.map((district) => ({ value: district.qc }));
      cb(results);
    },
    createFilter(queryString) {
      return (district) => {
        return (
          district.qc.toLowerCase().indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    // 取消按钮
    cancel() {
      this.$emit("update:modalType", "");
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.formData = {
        visitingName: "",
        damagedAmount: "",
        location: "",
        personLiable: "",
        controlUnit: "",
        caseName: "",
        petitionBehavior: "",
      };
      this.resetForm("form");
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.modalType == MODALTYPE["修改"]) {
            updatePetition(this.formData).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit("update:modalType", "");
              this.$emit("fatherMethod");
            });
          } else if (this.modalType == MODALTYPE["新增"]) {
            addPetition(this.formData).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit("update:modalType", "");
              this.$emit("fatherMethod");
            });
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/dialog.scss"; //模态框样式

::v-deep.treeselect {
  font-size: 12px !important;
  font-family: $font-family !important;
  font-weight: $form-label-font-weight !important;
}
</style>
